package com.example.biketestsotwo;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bikeparseutils.BikeTestMy;

public class MainActivity extends AppCompatActivity {

    BikeTestMy bikeTestMy = new BikeTestMy();
    private Handler handler = new Handler();
    private String[] testData = {
            "520D2AD2640082099C0003005700E10A",
            "520D2AD2640084089C0003004E00D90A",
            "520D2AD264006D099E0003005700CE0A",
            "520D2AD2640012099E00030053006F0A",
            "520D2AD26400BC089C0003005000130A",
            "520D2AD264006109A60003005600C90A"
    };

    private int currentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 开始测试，每1000ms发送一次数据
        startTesting();
    }

    private void startTesting() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // 检查是否还有数据要发送
                if (currentIndex < testData.length) {
                    // 获取当前要测试的数据
                    String input = testData[currentIndex];

                    // 调用 parseCommand 函数并打印结果
                    String result = bikeTestMy.parseCommand(input);
                    Log.d("UARTBIKE", "Parsed Result: " + result);

                    // 更新索引，准备发送下一组数据
                    currentIndex++;

                    // 继续调度下一次发送
                    handler.postDelayed(this, 1000);
                }
            }
        }, 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 停止 handler 的调度，避免内存泄漏
        handler.removeCallbacksAndMessages(null);
    }
}



